/*
 * universe.h
 *
 *  Created on: Jun 3, 2011
 *      Author: tqlong
 */

#ifndef UNIVERSE_H_
#define UNIVERSE_H_

/** Information about all random variables */
class Universe : protected std::vector<int>
{
  typedef std::vector<int> __Base;
  std::vector<int> value_;
public:
  Universe()
  {
  }

  Universe&
  addVariable(int cardinality)
  {
    push_back(cardinality);
    value_.push_back(-1); // a free variable
    return *this;
  }

  int
  cardinality(int varIdx) const
  {
    return this->at(varIdx);
  }

  int
  n_var() const
  {
    return this->size();
  }

  int
  varIdx(int i) const
  {
    return i;
  }

  void
  setValue(int varIdx, int value)
  {
    value_[varIdx] = value;
  }

  int
  value(int varIdx) const
  {
    return value_[varIdx];
  }

  Universe
  freeAll() const
  {
    Universe u = *this;
    std::fill(u.value_.begin(), u.value_.end(), -1);
    return u;
  }
};

template<typename OS>
  OS&
  operator<<(OS& os, const Universe& u)
  {
    if (u.n_var() == 0) return os;
    for (int i = 0; i < u.n_var(); i++)
      os << "(" << i << "," << u.cardinality(i) << "," << u.value(i) << ") ";
    return os;
  }
#endif /* UNIVERSE_H_ */
